<?php

/**
 * Created by PhpStorm.
 * User: allen_ios
 * Date: 17/6/13
 * Time: 上午10:23
 */
class BaseGoodsService extends BaseService
{
    const REDIS_CACHE_EXPIRE = 600;

    /**
     * 获得菜品列表信息，默认从缓存中去获取
     * @param $category_id
     * @param $restaurant_id
     * @return array
     */
    public function getGoods($category_id,$res_id)
    {

        $cacheKey =  'zdu_restaurantstyy_category_goods_'.$category_id.'_store';

        $cacheData = RedisService::get($cacheKey);

        if ($cacheData) {
            return $cacheData;
        }

        $mGoods = D('Base/RestaurantstyyGoods');

        $mRestaurantWeekRelation = D('Base/RestaurantstyyWeekRelation');

        // 获取星期几
        $weekNumber = date("w");

        $goodsIdArray = $mRestaurantWeekRelation->where(array('week'=>$weekNumber,'res_id'=>$res_id))->getField('goods_id',true);
       
        $goodsIdArray = implode(',',$goodsIdArray);
       
        $condition = array(
            'category_id'=>$category_id,
            'goods_sum'=>array('gt',0),
            'goods_id'=>array('in',$goodsIdArray),
            'cut_off'=>0
        );
        
        $info=$mGoods->getList($condition,'goods_id,goods_name,price,goods_logo,goods_sum');
      
        if ($info) {
            RedisService::set($cacheKey,json_encode($info),self::REDIS_CACHE_EXPIRE);
        }

        return $info;

    }

    /**
     * 获取热门菜品
     * @param $restaurant_id
     * @return array|mixed
     */
    public function getHotGoods($restaurant_id,$terminal){

        /*$cacheKey =  'zdu_restaurant_hot_goods_'.$restaurant_id.'_store';
        
        $cacheData = RedisService::get($cacheKey);
        if ($cacheData) {
            return $cacheData;
        }*/
        
        $mGoods = D('Base/RestaurantstyyGoods');
        $mOrderGoods = D('Base/RestaurantstyyOrderGoods');
        $WindowModel = D('Base/RestaurantstyyWindow');
        $CategoryModel = D('Base/RestaurantstyyGoodsCategory');

        $windowId = $WindowModel->where(array('restaurant_id'=>$restaurant_id,'cut_off'=>0))->field('window_id')->select();
        foreach($windowId as $k=>$v){
            $windowIds[] = $v['window_id'];
        }
        $windowIds = implode(',',$windowIds);

        $categoryId = $CategoryModel->where(array('window_id'=>array('in',$windowIds),'cut_off'=>0,'restaurant_id'=>$restaurant_id))->field('category_id')->select();

        foreach($categoryId as $k=>$v){
            $categoryIds[] = $v['category_id'];
        }
        $categoryIds = implode(',',$categoryIds);

        // 过去一周的时间戳
        $time = mktime(0, 0, 0, date('m'), date('d') - 6, date('Y'));
        $condition = array(
            'create_time'=>array('egt',$time),
            'restaurant_id'=>$restaurant_id
        );
        $limit = 12;
        //select sum(goods_num) as sale_number,goods_id from gc_restaurant_order_goods where create_time>=1465805339 group by goods_id order by sale_number desc limit 12;
        $fields = 'sum(goods_num) as sale_number,goods_id,print_type';
        $group = 'goods_id';
        $orderBy = 'sale_number desc';
        $list = $mOrderGoods->field($fields)->where($condition)->group($group)->order($orderBy)->limit($limit)->select();
        $info = array();
        foreach ($list as $key=>$value){
            $goodsInfo = $mGoods->where(array('goods_id'=>$value['goods_id'],'category_id'=>array('in',$categoryIds),'goods_sum'=>array('gt',0),'cut_off'=>0))->field('goods_id,goods_name,price,goods_logo,goods_sum,print_type')->find();
            if($goodsInfo){
                $info[] = $goodsInfo;
            }
        }

        $hot = array(
            'category_id'=>0,
            'category_name'=>'热门菜系',
            'goods'=>$info
        );
        /*RedisService::set($cacheKey,json_encode($hot),self::REDIS_CACHE_EXPIRE);*/
        return $hot;
    }
    

    /**
     * 获得单个商品详情
     * @param $goodsId
     */
    public function getGoodsInfo($resId,$restaurantId,$goodsId)
    {
        $cacheKey = "zdu_xlhstyy_goods_{$resId}_{$restaurantId}_{$goodsId}";
        $goodsInfo = RedisService::hget($cacheKey);

        if(!$goodsInfo){
            $mGoods = D('Base/RestaurantstyyGoods');
            $goodsInfo = $mGoods->field('goods_id,goods_name,price,goods_logo,category_id,print_type')->where(array('goods_id' => $goodsId))->find();
            //目前只有笑乐惠提供物品缓存
            /*RedisService::hset($cacheKey,$goodsInfo);*/

        }
        
        return $goodsInfo;
        
    }


    public function checkStorage($goods_id,$num){

        $mGoods = D('Base/RestaurantstyyGoods');


        $res = $mGoods->field('goods_id,goods_name,price')->where(array('goods_id' => $goods_id,'goods_sum'=>array('egt',$num)))->find();
        
        
        return $res;
        
    }

    public function checkOrderStorage($data){
        $mGoods = D('Base/RestaurantstyyGoods');
        $noGoods = array();
        foreach ($data as $k=>$v){
            $res = $mGoods->where(array('goods_id' => $v['goods_id']))->find();
            if($res['goods_sum']<=0||(int)$res['goods_sum']<$v['goods_num']){
                $noGoods[] = $res;
            }
        }
        if(count($noGoods)>0){
            return $noGoods;
        }
        return true;
    }

    /*
     *  根据菜单id来操作库存量
     * @param $goods_id int 菜品id
     * @param $num int 数量
     * @param $type 操作类型,1为减少库存,2为增加库存
     * @return array 返回类型
     */
    public function oprateStorage($goods_id,$num,$type){

        $mGoods = D('Base/RestaurantstyyGoods');

        $res = false;
        if ($type==1){//减库存

            $res =$mGoods->where(array('goods_id' => $goods_id))->setDec('goods_sum',$num);

        }elseif ($type==2){//加库存

            $res =$mGoods->where(array('goods_id' => $goods_id))->setInc('goods_sum',$num);
        }
        return $res;
    }

    /**
     *加销量，减库存
     * @param string $order_id
     */
    public function changeGoodsNum($order_id){
        $info = D('Base/RestaurantstyyOrderGoods')->getList(array('order_id' => $order_id));
        if($info){
            foreach($info as $v){
                D('Base/RestaurantstyyGoods')->changeNum(array('goods_id' => $v['goods_id']),$v['goods_num']);
            }
        }
    }
}